package xyf.p02.stack_queue;

import java.util.Stack;

/**
 * ClassName: M0305_StackSort
 * Description:
 * Author: xyf
 * Date: 9/3/22 6:21 PM
 * Version: 1.0
 **/
public class M0305_StackSort
{
    class SortedStack
    {
        Stack<Integer> stack = new Stack<>();
        Stack<Integer> help = new Stack<>();

        public SortedStack()
        {

        }

        public void push(int val)
        {
            if (stack.isEmpty())
            {
                stack.push(val);
                return;
            }

            if (val <= stack.peek())
            {
                stack.push(val);
            }
            else
            {
                while (!stack.isEmpty() && val > stack.peek())
                {
                    help.push(stack.pop());
                }
                stack.push(val);
                while (!help.isEmpty())
                {
                    stack.push(help.pop());
                }
            }
        }

        public void pop()
        {
            if (!stack.isEmpty())
            {
                stack.pop();
            }
        }

        public int peek()
        {
            if (!stack.isEmpty())
            {
                return stack.peek();
            }
            return -1;
        }

        public boolean isEmpty()
        {
            return stack.isEmpty();
        }
    }
}
